package com.aqielife.demo.service;

import com.aqielife.demo.dao.KnowRepository;
import com.aqielife.demo.dao.UserRepository;
import com.aqielife.demo.entity.Know;
import com.aqielife.demo.entity.UserNode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Optional;

/**
 * @author aqie
 * @date 2022/02/05 21:49
 * @desc
 */
@Service
public class UserServiceImpl implements UserService {
  @Autowired
  private UserRepository userRepository;

  @Autowired
  private KnowRepository knowRepository;

  @Override
  public UserNode create(UserNode userNode) {
    return userRepository.save(userNode);
  }

  @Override
  public void deleteById(Long id) {
    userRepository.deleteById(id);
  }

  @Override
  public Optional<UserNode> findById(long id) {
    return userRepository.findById(id);
  }

  @Override
  public List<UserNode> findAll() {
    return (List<UserNode>) userRepository.findAll();
  }

  @Override
  public Know addIKnows(UserNode fromNode, UserNode toNode) {
    Know newKnow = new Know(fromNode,toNode);
    return knowRepository.save(newKnow);
  }

  @Override
  public UserNode updateByNode(UserNode userNode) {
    return userRepository.updateByNode(userNode);
  }

  @Override
  public void deleteKnowByNodeId(long fromId, long toId) {
    knowRepository.deleteByNodeId(fromId,toId);
  }
}
